package com.ys.otms.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ys.otms.dao.IYLClassificationDao;
import com.ys.otms.dto.ParamDto;
import com.ys.otms.dto.ShipInfoDto;
import com.ys.otms.dto.YLClassificationDto;
import com.ys.otms.services.IYLClassificationServices;
import com.ys.otms.utils.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/10/12.
 */
@Service
public class YLClassificationServiceImpl implements IYLClassificationServices {
    private static Logger logger = LoggerFactory.getLogger(YLClassificationServiceImpl.class);
    @Autowired
    private IYLClassificationDao iylClassificationDao;

    static Map<Integer,String> shipState = new HashMap<>();
    static {
        shipState.put(0,"在航（主机推)");
        shipState.put(1,"锚泊");
        shipState.put(2,"失控");
        shipState.put(3,"操纵受限");
        shipState.put(4,"吃水受限");
        shipState.put(5,"靠泊");
        shipState.put(6,"搁浅");
        shipState.put(7,"捕捞作业");
        shipState.put(8,"靠帆船提供动力");
        shipState.put(9,"为将来 HSC 航行状态修正保留");
        shipState.put(10,"为将来 WIG 航行状态修正保留");
    }


    @Override
    public JSONObject insertYLClassificationQuery(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("YLClassificationServiceImpl insertYLClassificationQuery start obj{}",obj);
        String classificationName = JsonUtil.getJSONObjectString(obj,"classificationName");
        JSONObject jsonObject = new JSONObject();
        ParamDto paramDto = new ParamDto();
        YLClassificationDto ylClassificationDto =null;
        if (StringUtil.isNotEmpty(classificationName)){
            paramDto.setClassificationName(classificationName);
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","运力分类名称不能为空！");
            return jsonObject;
        }
        ylClassificationDto = iylClassificationDao.selectYLClassificationByName(paramDto);
        if (ylClassificationDto ==null){
            ylClassificationDto = new YLClassificationDto();
            ylClassificationDto.setClassificationName(classificationName);
            ylClassificationDto.setCompanyid(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
            ylClassificationDto.setCreateTime(new Timestamp((new Date()).getTime()));
            iylClassificationDao.insertYLClassificationQuery(ylClassificationDto);
            jsonObject.put("SUCCESS",true);
            jsonObject.put("MSG","运力分类新增成功");
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","账户中已有该运力分类！");
        }
        return jsonObject;
    }

    @Override
    public JSONObject selectShipsByYLClassification(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("YLClassificationServiceImpl selectShipsByYLClassification start obj{}",obj);
        String classificationId = JsonUtil.getJSONObjectString(obj,"classificationId");
        String shipName = JsonUtil.getJSONObjectString(obj,"shipName");
        String page =JsonUtil.getJSONObjectString(obj,"page");
        String limit = JsonUtil.getJSONObjectString(obj,"limit");
        ParamDto param = new ParamDto();
        JSONObject jsonObject = new JSONObject();
        param.setCompanyId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
        if (StringUtil.isNotEmpty(classificationId)){
            param.setClassificationId(Integer.parseInt(classificationId));
        }
        if (StringUtil.isNotEmpty(shipName)){
            param.setShipName(shipName);
        }
        param.setEndTime(new Timestamp((new Date()).getTime()));
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<ShipInfoDto> shipInfoDtoList = iylClassificationDao.selectShipsByYLClassification(param);
        PageInfo pageInfo = new PageInfo(shipInfoDtoList);
        jsonObject.put("pageInfo",pageInfo);
        jsonObject.put("shipInfoDtoList",shipInfoDtoList);
        jsonObject.put("SUCCESS",true);
        return jsonObject;
    }

    @Override
    public JSONObject deleteYLClassification(HttpServletRequest request) throws Exception {
        JSONObject obj =JsonUtil.getJSONObject(request);
        logger.info("YLClassificationServiceImpl deleteYLClassification start obj{}",obj);
        String classificationId =JsonUtil.getJSONObjectString(obj,"classificationId");
        JSONObject jsonObject = new JSONObject();
        if (StringUtil.isNotEmpty(classificationId)){
            ParamDto param = new ParamDto();
            param.setClassificationId(Integer.parseInt(classificationId));
            iylClassificationDao.deleteIYLShips(param);
            iylClassificationDao.deleteYLClassification(param);
            jsonObject.put("SUCCESS",true);
            jsonObject.put("MSG","删除成功！");
        }else{
            jsonObject.put("SUCCESS",false);
            jsonObject.put("MSG","请选中运力分类！");
        }
        return jsonObject;
    }

    @Override
    public JSONObject selectYLClassification(HttpServletRequest request) throws Exception {
        JSONObject jsonObject = new JSONObject();
        ParamDto paramDto = new ParamDto();
        Integer companyId = Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID"));
        paramDto.setUserId(companyId);
        List<YLClassificationDto> ylClassificationDtoList = iylClassificationDao.selectYLClassification(paramDto);
        jsonObject.put("ylClassificationDtoList",ylClassificationDtoList);
        jsonObject.put("SUCCESS",true);
        return jsonObject;
    }

    @Override
    public JSONObject selectNotClassificationShips(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("YLClassificationServiceImpl selectNotClassificationShips start obj{}",obj);
        String page =JsonUtil.getJSONObjectString(obj,"page");
        String limit = JsonUtil.getJSONObjectString(obj,"limit");
        String shipName = JsonUtil.getJSONObjectString(obj,"shipName");
        String classificationId =JsonUtil.getJSONObjectString(obj,"classificationId");
        ParamDto param = new ParamDto();
        PageUtil.setPage(Integer.parseInt(page),Integer.parseInt(limit),param);
        if (StringUtil.isNotEmpty(shipName)){
            param.setShipName(shipName);
        }
        if (StringUtil.isNotEmpty(classificationId)){
            param.setClassificationId(Integer.parseInt(classificationId));
        }
        param.setEndTime(new Timestamp((new Date()).getTime()));
        param.setUserId(Integer.parseInt((String) request.getSession().getAttribute("SESSION_COMPANY_ID")));
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        List<ShipInfoDto> shipInfoDtoList = iylClassificationDao.selectNotClassificationShips(param);
        PageInfo pageInfo = new PageInfo(shipInfoDtoList);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("shipsList",shipInfoDtoList);
        jsonObject.put("pageInfo",pageInfo);
        jsonObject.put("SUCCESS",true);
        return jsonObject;
    }
}
